﻿
var mongoose = require('mongoose');

var passwordField = {
	type: String,
	required: '密码未填写。',
	validate: [function(value) {
		return /^.{6,35}$/.test(value);
	}, '密码格式错误。密码长度在6-35个字符。']
};

var phoneField = {
	type: String,
	required: '手机号码未填写。',
	validate: [function(value) {
		return /^(13|15|18|17|14)[0-9]{9}$/.test(value);
	}, '手机号格式错误。请填写正确11位手机号码。'],
	unique: true
};

var tokenField = {
	key: String,
	expire: Date
};

var rolesField = {
	type: [String],
	required: '请至少选择一个角色。',
	validate: [function(value) {
		return value && value.length > 0;
	}, '请至少选择一个角色。']
};

// 雇主实体
var Employer = new mongoose.Schema({
	password: String,
	phone: phoneField,
	wallet: {
		balance: Number
	},
	createTime: {
		type: Date,
		default: new Date()
	},
	updateTime: {
		type: Date,
		default: new Date()
	},
	loc: {
		type: {
			lng: Number,
			lat: Number
		},
		index: '2dsphere'
	},
	token: tokenField,
	rongToken: String
}, {
	validateBeforeSave: false,
	safe: true
});

// 服务员实体
var Waiter = new mongoose.Schema({
	password: passwordField,
	phone: phoneField,
	loc: {
		type: [Number],
        index: '2dsphere'
	},
	sex: {
		type: String,
		required: '性别未选择。',
		validate: [function(value) {
			return /^[男|女]$/.test(value);
		}, '性别未选择。']
	},
	name: {
		type: String,
		required: '姓名未填写。',
		validate: [function(value) {
			return /^[\u4e00-\u9fa5]{2,10}$/.test(value);
		}, '请使用中文填写姓名，长度在2至10个字符。']
	},
	photo: {
		type: String,
		required: '未上传个人照片。',
		validate: [function(value) {
			return /^\w{24}$/.test(value);
		}, '未上传个人照片。']
	},
	idCardFront: {
		type: String,
		required: '未上传身份证正面照片。',
		validate: [function(value) {
			return /^\w{24}$/.test(value);
		}, '未上传身份证正面照片。']
	},
	idCardBack: {
		type: String,
		required: '未上传身份证背面照片。',
		validate: [function(value) {
			return /^\w{24}$/.test(value);
		}, '未上传身份证背面照片。']
	},
	idNum: {
		type: String,
		required: '未填写身份证号码。',
		validate: [function(value) {
			return /^(\d{15})|(\d{17}[0-9]|X)$/.test(value);
		}, '身份证号码格式错误。']
	},
	bankId: {
		type: String,
		required: '未填写银行帐号。',
		validate: [function(value) {
			return /^\d+$/.test(value);
		}, '银行帐号格式错误。']
	},
	bankPerson: {
		type: String,
		required: '未填写银行开户人。',
		validate: [function(value) {
			return /^[\u4e00-\u9fa5]{2,10}$/.test(value);
		}, '请使用中文填写银行开户人，长度在2至10个字符。']
	},
	bankName: {
		type: String,
		required: '未填写开户行银行名称。',
		validate: [function(value) {
			return /^[\u4e00-\u9fa5]{4,50}$/.test(value);
		}, '请使用中文填写开户行银行名称，长度在4至50个字符。']
	},
	trades: {
		type: [String],
		required: '请选择工种。',
		validate: [function(value) {
			return value && value.length;
		}, '请选择工种。']
	},
	hometown: {
		type: String,
		required: '籍贯未选择。',
		validate: [function(value) {
			return value && value.length;
		}, '籍贯未选择。']
	},
	ageLevel: {
		type: String,
		required: '请选择年龄范围。',
		validate: [function(value) {
			return value && value.length;
		}, '请选择年龄范围。']
	},
	empiricLevel: {
		type: String,
		required: '请选择经验范围。',
		validate: [function(value) {
			return value && value.length;
		}, '请选择经验范围。']
	},
	eduLevel: {
		type: String,
		required: '请选择学历。',
		validate: [function(value) {
			return value && value.length;
		}, '请选择学历。']
	},
	master: {
		type: String
	},
	approval: {
		type: Boolean,
		default: false
	},
	createTime: {
		type: Date,
		default: new Date()
	},
	updateTime: {
		type: Date,
		default: new Date()
	},
	token: tokenField,
	rongToken: String
}, {
	validateBeforeSave: false,
	safe: true
});

// 客服
var Customer = new mongoose.Schema({
	password: passwordField,
	phone: phoneField,
	name: {
		type: String,
		required: '名称未填写。',
		validate: [function(value) {
			return /^.{2,10}$/.test(value);
		}, '名称格式错误。请填写2到10个字符长度的名称。']
	},
	roles: rolesField,
	createTime: {
		type: Date,
		default: new Date()
	},
	updateTime: {
		type: Date,
		default: new Date()
	},
	token: tokenField,
	rongToken: String
}, {
	validateBeforeSave: false,
	safe: true
});

// 主管实体
var Master = new mongoose.Schema({
	password: passwordField,
	phone: phoneField,
	name: {
		type: String,
		required: '名称未填写。',
		validate: [function(value) {
			return /^.{2,10}$/.test(value);
		}, '名称格式错误。请填写2到10个字符长度的名称。']
	},
	roles: rolesField,
	createTime: {
		type: Date,
		default: new Date()
	},
	updateTime: {
		type: Date,
		default: new Date()
	},
	token: tokenField,
	rongToken: String
}, {
	validateBeforeSave: false,
	safe: true
});

// 订单实体
var Order = new mongoose.Schema({
	employer: {
		type: String
	},
	waiter: {
		type: String
	},
	trades: {
		type: String,
		required: '请选择工种。'
	},
	price: {
		type: {
			name: String,
			value: Number,
			time: Number
		},
		required: '请选择服务价格。',
		validate: [function(value) {
			return value && value.name && value.value && value.time;
		}, '请选择服务价格。']
	},
	reserveTime: {
		type: Date,
		required: '请选择服务时间。',
		validate: [function(value) {
			var limit = new Date();
			limit.setHours(limit.getHours() + BASE_CONFIG.reserveOrderTimeLimit);
			console.log('limit : ', limit);
			console.log('value : ', value);
			return limit < value;
		}, '预约时间需要提前' + BASE_CONFIG.reserveOrderTimeLimit + '小时。']
	},
	createTime: {
		type: Date,
		default: new Date()
	},
	distributionTime: {
		type: Date
	},
	confirmTime: {
		type: Date
	},
	startTime: {
		type: Date
	},
	finshTime: {
		type: Date
	},
	payTime: {
		type: Date
	},
	autoPayTime: {
		type: Date
	},
	complaintTime: {
		type: Date
	},
	cancelTime: {
		type: Date
	},
	revokeTime: {
		type: Date
	},
	paySum: {
		type: Number
	},
	status: {
		type: String
	},
	actionHistories: [{
		action: String,
		time: {
			type: Date,
			default: new Date()
		},
		accountType: String,
		accountId: String
	}]
}, {
	validateBeforeSave: false,
	safe: true
});

// 交易实体
var Transaction = new mongoose.Schema({
	type: String,
	payer: String,
	payee: String,
	money: Number,
	status: String,
	orders: [String],
	remark: {
		type: String,
		validate: [function(value) {
			return /^.{2,200}$/.test(value);
		}, '备注格式错误。请填写2到200个字符长度的名称。']
	},
	createTime: {
		type: Date,
		default: new Date()
	},
	payTime: {
		type: Date
	},
	confirmTime: {
		type: Date
	},
	cancelTime: {
		type: Date
	},
	actionHistories: {
		type: [{
			action: String,
			time: {
				type: Date,
				default: new Date()
			},
			accountType: String,
			accountId: String
		}]
	}
}, {
	validateBeforeSave: false,
	safe: true
});

// 配置实体
var Config = new mongoose.Schema({
	trades: [{
		name: String,
		icon: String,
		price: [{
			name: String,
			money: Number,
			time: Number
		}]
	}],
	ageLevel: [String],
	empiricLevel: [String],
	eduLevel: [String],
	provinces: [String]
}, {
	validateBeforeSave: false,
	safe: true
});

// 文件实体
var File = new mongoose.Schema({
	originalFilename: String,
	path: String,
	md5: String,
	size: Number,
	name: String,
	type: String
});

// 管理系统菜单实体
var SystemMenu = new mongoose.Schema({
	name: {
		type: String,
		required: '菜单名称必须填写。',
		validate: [function(value) {
			return /^.{2,10}$/.test(value);
		}, '菜单名称长度在2-10个字符。']
	},
	path: {
		type: String,
		required: '菜单路径必须填写。',
		validate: [function(value) {
			return /^.{1,100}$/.test(value);
		}, '菜单路径长度在1-100个字符。']
	},
	roles: rolesField
});

var initDb = function() {
	// 连接数据库
	var mongo = mongoose.connect('mongodb://localhost/housekeeping');

	// 定义全局数据库对象
	global.Db = {
		Employer: mongo.model('Employer', Employer),
		Waiter: mongo.model('Waiter', Waiter),
		Customer: mongo.model('Customer', Customer),
		Master: mongo.model('Master', Master),
		Order: mongo.model('Order', Order),
		Transaction: mongo.model('Transaction', Transaction),
		Config: mongo.model('Config', Config, 'config'),
		File: mongo.model('File', File),
		SystemMenu: mongo.model('SystemMenu', SystemMenu)
	};
};

module.exports = initDb;
